package it.disim.tlp.webReputation.logic;

import it.disim.tlp.webreputation.aggregatorplugin.ManifestLoader;
import it.disim.tlp.webreputation.aggregatorplugin.ManifestedPlugin;
import it.disim.tlp.webreputation.aggregatorplugin.PluginLoader;
import it.disim.tlp.webreputation.dispatcher.AggregatorThread;
import it.disim.tlp.webreputation.exceptions.AggregatorLogger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.SAXException;

public class DataAggregator {
	public static void main(String[] args){
		
		/*plugins, threads, and mainifested plugins lists*/
		List<String> plugins = new ArrayList<String>();
		plugins.add("SimplePlugin"); /*da prendere dal db... solo test*/
		plugins.add("AdvancedPlugin");
		plugins.add("AdvancedPlugin2");
		List<AggregatorThread> threads = new ArrayList<AggregatorThread>();
		List<ManifestedPlugin> pluginsList = new ArrayList<ManifestedPlugin>();
		
		
		/*plugins loading at beginning phase if any*/
		for(String plg : plugins){
			
			/*manifest loading  and checking if it's valid*/
			ManifestLoader ml = null;
			try {
				ml = new ManifestLoader("plugins/"+plg+"/Manifest.xml");
			} catch (SAXException | IOException e1) {
				System.out.println("Manifest "+plg+" is not valid... check log file inside logs directory.");
				AggregatorLogger.logging(plg, e1);
				continue;
			}
			
			/* plugin loading and checking if it's execute runtime errors during that phase*/
			try{
				ManifestedPlugin mp = new ManifestedPlugin(new PluginLoader(plg,ml.getJarName(),ml.getLibs(),ml.getClassName()).getPlugin(),ml);
				pluginsList.add(mp);
				System.out.println("Plugin "+mp.getManifest().getName()+" started!");
				AggregatorThread at = new AggregatorThread(mp);
				threads.add(at);
				at.start();
			}catch(Exception e){
				System.out.println("Plugin "+ml.getName()+" not loaded! Errors during loading... check log file inside logs directory.");
				AggregatorLogger.logging(ml.getName(), e);
			}
		}
	}
}
